package org.xtframe.sql.verify;

import java.util.List;

import net.sf.json.JSONObject;

import org.xtframe.common.SimpleFormat;
import org.xtframe.sql.SqlDataVerify.VerifyType;
import org.xtframe.sql.PreparedParameter;
import org.xtframe.sql.VerifyBase;
import org.xtframe.util.StringUtil;

public class PresenceAddVerify implements VerifyBase {
	
	private int position = -1;
	
	private String format = null;
	
	private String defaultValue = "";

	public void init(JSONObject data) {
		if(data.has("position")) this.position = data.optInt("position");
		if(data.has("format")) this.format = data.optString("format");
		if(data.has("default")) this.defaultValue = data.optString("default");
	}

	public int getPosition() {
		return this.position;
	}

	public VerifyType getXtype() {
		return VerifyType.replace;
	}

	public boolean check(List<String> parameters) {
		return false;
	}

	public String replace(List<String> parameters, List<PreparedParameter> preparedParameters) {
		String o = parameters.get(position);
		if(o==null || o.equals("")){
			return this.defaultValue;
		} else {
			return SimpleFormat.format(this.format, StringUtil.replaceSql(o));
		}
	}

}
